requirejs.config({
    baseUrl: '../js/libs',
    paths: {
    	jquery : 'jquery.min',
        bootstrap: "../../bootstrap-3.3.5/dist/js/bootstrap.min",
        config: "../common/config",
        common: "../common/common",
        validform:"../plugins/Validform/Validform_v5.3.2",
        layer: '../plugins/layer/layer',
        arttemplateUtil: '../common/arttemplate-util',
        
        filer: '../plugins/fileinput/js/new/jquery.filer'
    },
    shim: { //引入没有使用requirejs模块写法的类库。backbone依赖underscore
        'jquery': {
            exports: '$'
        },
        'bootstrap': ['jquery'],
        'filter':['jquery']
    }
});
requirejs(['jquery', 'bootstrap', 'config', 'arttemplateUtil', 'common', 'filer','validform'], function($, bootstrp, config, arttemplateUtil, common, filer, validform) {
	
	//加载扩展模块
	layer.config({
		extend: '../plugins/layer/extend/layer.ext.js'
	});
	
	/**
     * Created by linyamei on 2016/8/25.
     */
    var app = {
        init: function() {
        	
            app.rendTemple();
        },
        
        
        /**
         * 
         * @return {[type]} [description]
         */
        rendTemple: function() {
        	
        	arttemplateUtil.template.helper('compareSize', function(size,index){  
                if(parseInt(size) >= parseInt(index)) return true;
                return false;
        	});
        	
        	common.common_ajax_request('/api/v1/queryStoreSlideList', {}, false, function(data){
        		dataJson={};
        		if(data==null || data==undefined || data.length==0) {
        			dataJson.listSize = data.length;
        		} else {
        			dataJson.list = data;
        			dataJson.imgRoot = config.imgRoot();
        			dataJson.listSize = data.length;
        		}
        		var store_slide_content_html = arttemplateUtil.template('store_slide_tpl', dataJson);
        		$(".store_slide_body").html(store_slide_content_html);
        		uploadInit('#fileinput1');
        		uploadInit("#fileinput2");
        		uploadInit("#fileinput3");
        		uploadInit("#fileinput4");
        		uploadInit("#fileinput5");
        		//表单验证
        		var storeSlideForm_demo = $("form[name=storeSlideForm]").Validform({
        			tiptype:4,
        			postonce:true,
        			isShowRight:false
        		});
        	    
        		commitBtnClick(storeSlideForm_demo);
        		deletePic();
        		
        		
    		});
        }

    }
    
    //编辑时移除自定义预览图片框
    var deletePic = function() {
        $(".shanchu").on('click', function() {
        	//重置上传到服务端的图片链接
            $(this).parents(".hidbox").find("input.accurl").val("");
        	//删除图片预览框
            $(this).parent().parent().remove();
        });
    }
    
    //点击提交按钮事件
    var commitBtnClick = function(storeSlideForm_demo) {
    	$("#commit").on("click",function(e){
    		e.preventDefault();
    		if(storeSlideForm_demo.check()) {
	    		var params = $("form[name=storeSlideForm]").serializeArray();
	    		var tip = window.layer.msg('提交中', {icon: 16});
	    		var commitThis = $(this);
	    		commitThis.attr('disabled',true);
	    		common.common_ajax_request('/api/v1/saveOrUpdateStoreSlideList',
	    				params, 
	    				false, 
	    				function(data){
			            	setTimeout(function(){
			            		window.layer.close(tip);
			            		window.layer.msg('提交成功', {
			            			  icon: 1,
			            			  time: 500 //如果不配置，默认是3秒
			            			}, function(){
			            				//刷新当前页面
			            				app.rendTemple();
			            			});   
			            			      
			          		}, 150);
						},'','get',function(){
							commitThis.removeAttr("disabled");
						});
    		}
    	});
    }
    
    //绑定图片上传事件
    var uploadInit = function(uploadId) {
    	$(uploadId).filer({
            changeInput: '<div class="jFiler-input-dragDrop mb10"><a class="jFiler-input-choose-btn blue-light btn btn-primary">选择图片</a></div>',
            limit: 1,
            maxSize: 2,
            extensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp'],
            showThumbs: true,
            theme: "dragdropbox",
            templates: {
                box: '<ul class="jFiler-items-list jFiler-items-grid"></ul>',
                item: '<li class="jFiler-item" style="width: 100%">\
                            <div class="jFiler-item-container">\
                                <div class="jFiler-item-inner">\
                                    <div class="jFiler-item-thumb">\
                                        <div class="jFiler-item-status"></div>\
                                        <div class="jFiler-item-info">\
                                            <span class="jFiler-item-title"><b title="{{fi-name}}">{{fi-name | limitTo: 25}}</b></span>\
                                            <span class="jFiler-item-others">{{fi-size2}}</span>\
                                        </div>\
                                        {{fi-image}}\
                                    </div>\
                                    <div class="jFiler-item-assets jFiler-row">\
                                        <ul class="list-inline pull-left">\
                                            <li>{{fi-progressBar}}</li>\
                                        </ul>\
                                        <ul class="list-inline pull-right">\
                                            <li><a class="icon-jfi-trash jFiler-item-trash-action"></a></li>\
                                        </ul>\
                                    </div>\
                                </div>\
                            </div>\
                        </li>',
                itemAppend: '<li class="jFiler-item" style="width: 49%">\
                                <div class="jFiler-item-container">\
                                    <div class="jFiler-item-inner">\
                                        <div class="jFiler-item-thumb">\
                                            <div class="jFiler-item-status"></div>\
                                            <div class="jFiler-item-info">\
                                                <span class="jFiler-item-title"><b title="{{fi-name}}">{{fi-name | limitTo: 25}}</b></span>\
                                                <span class="jFiler-item-others">{{fi-size2}}</span>\
                                            </div>\
                                            {{fi-image}}\
                                        </div>\
                                        <div class="jFiler-item-assets jFiler-row">\
                                            <ul class="list-inline pull-left">\
                                                <li><span class="jFiler-item-others">{{fi-icon}}</span></li>\
                                            </ul>\
                                            <ul class="list-inline pull-right">\
                                                <li><a class="icon-jfi-trash jFiler-item-trash-action"></a></li>\
                                            </ul>\
                                        </div>\
                                    </div>\
                                </div>\
                            </li>',
                progressBar: '<div class="bar"></div>',
                itemAppendToEnd: false,
                removeConfirmation: false,//删除是否提示
                _selectors: {
                    list: '.jFiler-items-list',
                    item: '.jFiler-item',
                    progressBar: '.bar',
                    remove: '.jFiler-item-trash-action'
                }
            },
            uploadFile: {
                url: config.apiRoot() + "/api/v1/storeUpload",
                data: config.setFullParameterArr({}),
                type: 'POST',
                enctype: 'multipart/form-data',
                beforeSend: function(){
                	var length = $(uploadId).parent().find(".jFiler-row").find(".jFiler-items-grid").find(".jFiler-item").length;
                	if(length > 1) {
                		$(uploadId).parent().find(".jFiler-row").find(".jFiler-items-grid").find(".jFiler-item:last").remove();
                	}
                	//上传的时候把编辑的图片预览窗隐藏
                	$(uploadId).parent().parent().find(".shanchu").parent().parent().hide();
                },
                success: function(data, el){
                	var jsondata = $.parseJSON(data); 
                	if(jsondata.code == "200") {
                		var parent = el.find(".jFiler-jProgressBar").parent();
                        el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
                            $("<div class=\"jFiler-item-others text-success\"><i class=\"icon-jfi-check-circle\"></i> Success</div>").hide().appendTo(parent).fadeIn("slow");
                        });
                        $(uploadId).parent().parent().find(".accurl").val(jsondata.data.path+jsondata.data.ext);
                	} else {
                		var parent = el.find(".jFiler-jProgressBar").parent();
                        el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
                            $("<div class=\"jFiler-item-others text-error\"><i class=\"icon-jfi-minus-circle\"></i> Error</div>").hide().appendTo(parent).fadeIn("slow");    
                        });
                        //上传失败的时候把编辑的图片预览窗显示
                    	//$(uploadId).parent().parent().find(".shanchu").parent().parent().show();
                	}
                    
                    //console.log(jsondata);
                },
                error: function(el){
                    var parent = el.find(".jFiler-jProgressBar").parent();
                    el.find(".jFiler-jProgressBar").fadeOut("slow", function(){
                        $("<div class=\"jFiler-item-others text-error\"><i class=\"icon-jfi-minus-circle\"></i> Error</div>").hide().appendTo(parent).fadeIn("slow");    
                    });
                    //上传失败的时候把编辑的图片预览窗显示
                	//$(uploadId).parent().parent().find(".shanchu").parent().parent().show();
                },
                statusCode: null,
                onProgress: null,
                onComplete: null
         },
		captions: {
			button: "Choose Files",
			feedback: "Choose files To Upload",
			feedback2: "files were chosen",
			drop: "Drop file here to Upload",
			removeConfirmation: "Are you sure you want to remove this file?",
			errors: {
				filesLimit: "只能选择 {{fi-limit}} 图片上传.",
				filesType: "只能上传图片.",
				filesSize: "{{fi-name}} 太大! 上传文件不超过{{fi-maxSize}} MB.",
				filesSizeAll: "选择的文件太大! 上传文件不超过 {{fi-maxSize}} MB."
			}
		}
      });
   }
    
    app.init();
});
